package com.example.demo.ws;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Configuration
@EnableWebSocket
public class WsConfigurer implements WebSocketConfigurer {
    private static final Logger log = LoggerFactory.getLogger(WsConfigurer.class);

    private static final String WS_PATH = "/ws/**";

    @Autowired
    private WsHandler wsHandler;
    @Autowired
    private WsInterceptor wsInterceptor;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        log.info("registerWebSocketHandlers: {}", WS_PATH);

        registry.addHandler(wsHandler, WS_PATH)
                .addInterceptors(wsInterceptor)
                .setAllowedOrigins("*");
    }
}